Recipes panda
Panda cssの概念
Create composable style variants that are typed and generates the atomic or layer recipe
Panda provides a way to write CSS-in-JS with better performance, developer experience, and composability. One of its key features is the ability to create multi-variant styles with a type-safe runtime API.
型安全 type-safeなruntime API を使用してmulti-variant styles マルチバリアント スタイルを作成できることです。
codeと感想
code:button.ts
import { cva } from '../styled-system/css'
// これ自身はTS世界の変数なので、型安全だし、持ち運べるのが良いね。
const button = cva({
base: {
display: 'flex'
},
variants: {
// semanticな名前にできるの良いね。
visual: {
solid: { bg: 'red.200', color: 'white' },
outline: { borderWidth: '1px', borderColor: 'red.200' }
},
size: {
sm: { padding: '4', fontSize: '12px' },
lg: { padding: '8', fontSize: '24px' }
}
}
code: using-button.tsx
import { button } from './button'
const Button = () => {
return (
// DOM側がスッキリするのが最高。
<button className={button({ visual: 'solid', size: 'lg' })}>
Click Me
</button>
)
}
code:output.css
// ちゃんとLayerで詳細度縛ってくれているの良い。
@layer utilities {
// あくまで利用されるpropのUtility classが生成されて渡されるので、パフォーマンス的にも良い。
.d_flex {
display: flex;
}
.bg_red_200 {
background-color: #fed7d7;
}
.color_white {
color: #fff;
}
.border_width_1px {
border-width: 1px;
}
/* ... */
}
recipesの置き場
componentのDOMの近くにファイルとして置くか?theme/recipes/xxxみたいに置くか?
themeにおいてる
疑問
Atomic Recipe pandaとConfig Recipe pandaの違いは?
When dealing with simple use cases, or if you need code colocation, or even avoiding dynamic styling, atomic recipes shine by providing all style variants. Config recipes are preferred for design system components, delivering leaner CSS with only the styles used. Choose according to your component needs.
Recipes - Panda CSS
Config Recipe pandaは、codeの仕様状況によって、Just In Time JITに生成するのでPerformance パフォーマンスがよい。
気軽に始めやすいのは、Atomic Recipe pandaであるが、主にcomponent 構成要素に対してrecipes利用されるので、Config Recipe pandaに寄せるが無難かな。
Project classっぽい概念は、あまりdefineRecipeすることなさそう。
xxx_section --xx-typeみたいなmodifier サブクラス cssでproject classを分ける概念をどう書くかのがbestか分かってない。その時、Atomic Recipe pandaが適切かもしれない。